package com.zcl.tool.utils;

import com.lowagie.text.Font;
import com.lowagie.text.Rectangle;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.rtf.RtfWriter2;

import java.awt.*;
import java.io.FileOutputStream;
import java.util.List;

/**
* @Description: 生成样本送检系统样品列表word
* @Param: 
* @return: 
* @Author: zhanchenlei
* @Date: 2021/6/2
*/
public class GenerateWord {
    public static void word(List list) throws Exception {
        BaseFont bfChinese = BaseFont.createFont("Courier", "Cp1252", BaseFont.NOT_EMBEDDED);

        //设置纸张的大小对象
        Rectangle rectangle = new Rectangle(PageSize.A4);
        // 创建word文档,并旋转，使其横向
        Document document = new Document(rectangle.rotate());
        RtfWriter2.getInstance(document, new FileOutputStream("D:/样本送检系统样品列表.doc"));
        document.open();
        //报表标题
        Paragraph headTitle = new Paragraph("样本送检系统样品列表");
        Font headFont = new Font(bfChinese);
        headFont.setSize(22);
        headFont.setStyle(Font.BOLD);
        headTitle.setFont(headFont);
        headTitle.setAlignment(1);//居中
        document.add(headTitle);

        //正文表格
        PdfPTable table = new PdfPTable(10);
        table.setWidths(new float[]{9f,9f,9f,20f,20f,20f,20f,9f,9f,9f});
        table.setTotalWidth(114);

        //设置表头字体样式
        Font tableHeadFont = new Font(bfChinese);//表头字体样式
        tableHeadFont.setSize(10.5f);//设置表头字体为五号字体
        tableHeadFont.setStyle(Font.BOLD);//加粗

        //表头第一列：序号
        PdfPCell headCell1 = new PdfPCell();
        Paragraph headCell1Phrase = new Paragraph("序号");
        headCell1Phrase.setFont(tableHeadFont);
        headCell1.setPhrase(headCell1Phrase);
        headCell1Phrase.setAlignment(1);
        headCell1.setHorizontalAlignment(1);

        //表头第二列：样本类型
        PdfPCell headCell2 = new PdfPCell();
        Paragraph headCell2Phrase = new Paragraph("样本类型");
        headCell2Phrase.setFont(tableHeadFont);
        headCell2.setPhrase(headCell2Phrase);
        headCell2Phrase.setAlignment(1);
        headCell2.setHorizontalAlignment(1);

        //表头第三列：样本数量
        PdfPCell headCell3 = new PdfPCell();
        Paragraph headCell3Phrase = new Paragraph("样本数量");
        headCell3Phrase.setFont(tableHeadFont);
        headCell3.setPhrase(headCell3Phrase);
        headCell3Phrase.setAlignment(1);
        headCell3.setHorizontalAlignment(1);

        //表头第四列：送检单位
        PdfPCell headCell4 = new PdfPCell();
        Paragraph headCell4Phrase = new Paragraph("送检单位");
        headCell4Phrase.setFont(tableHeadFont);
        headCell4.setPhrase(headCell4Phrase);
        headCell4Phrase.setAlignment(1);
        headCell4.setHorizontalAlignment(1);

        //表头第五列：送检日期
        PdfPCell headCell5 = new PdfPCell();
        Paragraph headCell5Phrase = new Paragraph("送检日期");
        headCell5Phrase.setFont(tableHeadFont);
        headCell5.setPhrase(headCell5Phrase);
        headCell5Phrase.setAlignment(1);
        headCell5.setHorizontalAlignment(1);

        //表头第六列：收样日期
        PdfPCell headCell6 = new PdfPCell();
        Paragraph headCell6Phrase = new Paragraph("收样日期");
        headCell6Phrase.setFont(tableHeadFont);
        headCell6.setPhrase(headCell6Phrase);
        headCell6Phrase.setAlignment(1);
        headCell6.setHorizontalAlignment(1);

        //表头第七列：出报告日期
        PdfPCell headCell7 = new PdfPCell();
        Paragraph headCell7Phrase = new Paragraph("出报告日期");
        headCell7Phrase.setFont(tableHeadFont);
        headCell7.setPhrase(headCell7Phrase);
        headCell7Phrase.setAlignment(1);
        headCell7.setHorizontalAlignment(1);


        //表头第八列：有无无法判断
        PdfPCell headCell8 = new PdfPCell();
        Paragraph headCell8Phrase = new Paragraph("有无无法判断");
        headCell8Phrase.setFont(tableHeadFont);
        headCell8.setPhrase(headCell8Phrase);
        headCell8Phrase.setAlignment(1);
        headCell8.setHorizontalAlignment(1);

        //表头第九列：收样样品是否破损
        PdfPCell headCell9 = new PdfPCell();
        Paragraph headCell9Phrase = new Paragraph("收样样品是否破损");
        headCell9Phrase.setFont(tableHeadFont);
        headCell9.setPhrase(headCell9Phrase);
        headCell9Phrase.setAlignment(1);
        headCell9.setHorizontalAlignment(1);

        //表头第十列：地区
        PdfPCell headCell10 = new PdfPCell();
        Paragraph headCell10Phrase = new Paragraph("地区");
        headCell10Phrase.setFont(tableHeadFont);
        headCell10.setPhrase(headCell10Phrase);
        headCell10Phrase.setAlignment(1);
        headCell10.setHorizontalAlignment(1);


        table.addCell(headCell1);
        table.addCell(headCell2);
        table.addCell(headCell3);
        table.addCell(headCell4);
        table.addCell(headCell5);
        table.addCell(headCell6);
        table.addCell(headCell7);
        table.addCell(headCell8);
        table.addCell(headCell9);
        table.addCell(headCell10);
        //表头添加完毕

        //添加表格体



        for (Object o : list) {
            table.addCell(String.valueOf(0));//里面是String类型
            /*table.addCell(String.valueOf(sampleIndexDTO.getId()));//ID
            table.addCell(sampleIndexDTO.getSampleType());//样本类型
            table.addCell(String.valueOf(sampleIndexDTO.getSampleQuantity()));//样本数量
            table.addCell(sampleIndexDTO.getSubmissionUnit());//送检单位
            table.addCell(sampleIndexDTO.getCheckTime());//送检日期
            table.addCell(sampleIndexDTO.getReceiveSampleTime());//收样日期
            table.addCell(sampleIndexDTO.getReportTime());//出报告日期
            table.addCell(String.valueOf(sampleIndexDTO.getClear()));//有无无法判断
            table.addCell(String.valueOf(sampleIndexDTO.getBroken()));//是否破损
            table.addCell(sampleIndexDTO.getLocation());//地区*/
        }

        document.add(table);


        //页脚段落
        Paragraph paraFooter = new Paragraph();
        Font font = new Font();
        //页脚的字体大小
        font.setSize(12f);
        font.setColor(new Color(0, 0, 0));
        paraFooter.setFont(font);
        paraFooter.setAlignment("center");
        //页脚的段落和是否有页码
        HeaderFooter footer = new HeaderFooter(paraFooter, true);
        //页脚的对齐方式（应该在footer设置而不是段落中设置）
        footer.setAlignment(1);
        document.setFooter(footer);

        document.close();
    }

    public static void main(String[] args) throws Exception {

    }
}